package com.sgb.kjwl.view.ui.fragment.material;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentAllStList3Binding;
import com.sgb.kjwl.di.material.DaggerMaterialFragComponent;
import com.sgb.kjwl.di.material.MaterialFragModule;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.ui.adapter.CommPagerAdapter3;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.MaterialEvent;
import com.sgb.kjwl.viewmodel.material.AllStModel3;
import com.swgk.core.base.di.AppComponent;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 商砼列表
 * cfk
 */
public class AllStFragment3 extends AppFragment {
    @Inject
    AllStModel3 viewModel;
    private FragmentAllStList3Binding binding;
    private double locCurrentLat = 0;
    private double locCurrentLong = 0;
    private String title;
    private String id = "";
    private String fId = "";  //用户筛选类别查询
    private int lastPosition = 0;
    private CommPagerAdapter3 pagerAdapter;
    private ArrayList<Fragment> mFragments;
    private String[] titles;
    private boolean isCreate = false;
    private boolean init = true;
    private boolean isVisible;
    private String threeLevelClassId = "";

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMaterialFragComponent.builder()
                .appComponent(appComponent)
                .materialFragModule(new MaterialFragModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (!isCreate) {
            return;
        }
        if (isVisibleToUser) {
            if (init) {
                init = false;
                initView();
                init();
            }
        }
    }

    public static AllStFragment3 start(String id, String mId, String threeLevelClassId) {
        AllStFragment3 fragment = new AllStFragment3();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        bundle.putString("mId", mId);
        bundle.putString("threeLevelClassId", threeLevelClassId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isCreate = true;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_all_st_list3, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        title = getArguments().getString("title");
        id = getArguments().getString("id");
        fId = getArguments().getString("mId");
        threeLevelClassId = getArguments().getString("threeLevelClassId");
        if (init && isVisible) {
            init = false;
            initView();
            init();
        }
    }

    private void initView() {
        binding.ivBack.setVisibility(View.VISIBLE);
        binding.ivBack.setOnClickListener(this);
        binding.rightView.setOnClickListener(this);
        binding.locView.setOnClickListener(this);
        binding.editSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.editSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    closeKeybord();
                    EventBus.getDefault().postSticky(MaterialEvent.make().setMessageTag(EventConstant.EVENT_ST_FILTER).setMessageData(binding.editSearch.getText().toString()));
                }
                return false;
            }
        });
    }

    private void init() {
        viewModel.materialFilterJson.get().setItemCid(fId);
        viewModel.materialFilterJson.get().setItemId(threeLevelClassId);
        viewModel.getFilterTypeModel(fId);
    }

    public void showTypeView(List<DictEntity3> datas) {
        if (datas == null || datas.size() == 0) {
            binding.viewpager.setVisibility(View.GONE);
            binding.emptyView.setVisibility(View.VISIBLE);
            return;
        }
        mFragments = new ArrayList<>();
        int size = datas.size();
        titles = new String[size];
        for (int i = 0; i < size; i++) {
            mFragments.add(STListFragment4.newInstance(id, fId, i));
            titles[i] = datas.get(i).getName();
            if (!TextUtils.isEmpty(threeLevelClassId) && threeLevelClassId.equals(datas.get(i).getId())) {
                lastPosition = i;
            }
        }
        pagerAdapter = new CommPagerAdapter3(getChildFragmentManager(), mFragments, titles);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.tablayout.setupWithViewPager(binding.viewpager);
        binding.viewpager.setCurrentItem(lastPosition);
    }
}